<?php
namespace app\admin\logic;

use think\exception\ValidateException;
use think\facade\View;

class UsersLogic
{
    public static function addUsersInfo($post)
    {
        $data = [
            'uname'    =>$post['name'],
            'author'    =>$post['author'],
        ];
        return $data;
    }
    public static function userWhere($post)
    {
        $where = [];
        if (isset($post['name']) && !empty($post['name'])){
            $where[] = ['uname','=',$post['name']];
        }
        if (isset($post['password']) && !empty($post['password'])){
            $where[] = ['upwd','=',md5($post['password'])];
        }
        return $where;
    }
    public static function usercLogin($post)
    {
        try{
            $res = validate(\app\admin\validate\Login::class)->scene('ycode')->check($post);
            $msg = "登录成功";
        }catch(ValidateException $e){
            $res = false;
            $msg = $e->getMessage();
        }
        return [$res,$msg];
    }
    public static function userLogin($post)
    {
        try{
            $res = validate(\app\admin\validate\Login::class)->scene('ncode')->check($post);
            $msg = "登陆成功";
        }catch(ValidateException $e){
            $res = false;
            $msg = $e->getMessage();
        }
        return [$res,$msg];
    }
    public static function reformData($param)
    {
        $where = [];
        if (isset($param['keywords']) && !empty($param['keywords'])){
            $where[] = ['uname','like','%'.$param['keywords'].'%'];
        }
        return $where;
    }
    public static function setData($param)
    {
        $data = [
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
        ];
        return $data;
    }
    public static function editUserInfo($param)
    {
        if ($param['reset']==1){
            $pwd = md5(12345);
        }else{
            $pwd = $param['upwd'];
        }
        $data = [
            'id'        =>$param['id'],
            'uname'     =>$param['uname'],
            'upwd'      =>$pwd,
            'email'     =>$param['email'],
            'tel'       =>$param['tel'],
            'author'    =>$param['author']
        ];
        return $data;
    }
    public static function addUserInfo($param)
    {
        $data = [
            'uname'     =>$param['uname'],
            'upwd'      =>md5($param['upwd']),
            'email'     =>$param['email'],
            'tel'       =>$param['tel'],
            'author'  =>$param['author']
//            'add_time'  =>time()
        ];
        return $data;
    }
}